<?php
class Db_Promotion
{
    public static function total($condition=false) {
        static $totals;
        $key = hash('crc32b', $condition);
        if(!isset($totals[$key]))
            $totals[$key] = DB::count('promotion', $condition, __FILE__ . ' line ' . __LINE__);
        return $totals[$key];
    }

    public static function getPromotions($limit='', $condition='')
    {
        static $promotions;
        $key = hash('crc32b', $condition.$limit);

        if(!isset($promotions[$key]))
        {
            if($condition)
            {
            	$condition = 'WHERE '.$condition;
            }

            $promotions[$key] =  DB::fetch_all("SELECT * FROM promotion $condition $limit", __FILE__ . ' line ' . __LINE__);
        }

        return $promotions[$key];
    }




    /**
     * @static
     * @param string $limit
     * @param string $condition
     * @return mixed | array id
     */
    public static function getPromotionsId($limit='', $condition='', $indexId = true){
        static $promotions;
        $key = hash('crc32b', $condition.$limit);

        if(!isset($promotions[$key]))
        {
            if($condition)
            {
                $condition = 'WHERE '.$condition;
            }
            if($indexId){
                $promotions[$key] =  DB::fetch_all("SELECT id FROM promotion $condition $limit", __FILE__ . ' line ' . __LINE__);
            }else{
                $data =  DB::fetch_all_array("SELECT id FROM promotion $condition $limit", __FILE__ . ' line ' . __LINE__);
                $arrId = array();
                if($data !== null){
                    foreach($data as $t){
                        $arrId[]=$t['id'];
                    }
                }
                return $arrId;
            }

        }

        return $promotions[$key];
    }

    public static function selectPromotions($cols='*', $limit='', $condition='') {
        static $promotions;
        $key = hash('crc32b', $cols.$condition.$limit);
        if(!isset($promotions[$key])) {
            if($condition) $condition = 'WHERE '.$condition;
            $promotions[$key] =  DB::fetch_all("SELECT $cols FROM promotion $condition $limit", __FILE__ . ' line ' . __LINE__);
        }
        return $promotions[$key];
    }

    public static function getPromotionById($id)
    {
        static $promotions;

        if(!isset($promotions[$id]))
        {
            $arrId = explode(',',$id);
            if(sizeof($arrId) >1){
                $promotions[$id] = DB::select('promotion', 'id IN ('.$id.')', __FILE__ . ' line ' . __LINE__);
            }else{
                $promotions[$id] = DB::select('promotion', 'id='.$id, __FILE__ . ' line ' . __LINE__);
            }

        }

        return $promotions[$id];
    }


    public static function insert($promotion) {
        //var_dump($hotel);die;
        $id = DB::insert('promotion', $promotion, false, __FILE__ . ' line ' . __LINE__);
        return $id;
    }

    public static function update($promotion, $id)
    {

        return DB::update_id('promotion', $promotion, $id, __FILE__ . ' line ' . __LINE__);
    }

    public static function delPromotionById($id)
    {
        return DB::update_id('promotion', array("status" =>2), $id, __FILE__ . ' line ' . __LINE__);
    }


    public static function delPromotions($ids)
    {
        return DB::update('promotion', array("status" =>2), 'id IN (' . implode(',', $ids) . ') AND status!=2', __FILE__ . ' line ' . __LINE__);
    }




}
